// pages/ryshop/ryshop.js
var wxCharts = require('../../utils/wxcharts-min.js');
var app = getApp();
var radarChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    if_iphone:true,
    goodsAarr:{},
    tupian: getApp().globalData.imgUrl,
    siteUrl: getApp().globalData.siteUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载数据中...',
      mask: true,
    });
    let that = this;
    let apiurl = 'goods/goodsList';
    console.log(apiurl);
    app.getdata.fetchApi(apiurl, {})
      .then(d => {
        if (d.status == 1) {
          // console.log(d.data)
          that.goodsAarr = d.data.shop;//
          this.setData({
            list: d.data.shop.goods,
            lists: d.data.shop.best_goods,
            share_points:d.data.share_points
          })
        } else {
          this.setData({
            list: '',
            lists: ''})
        }
      })
      .catch(e => {
        this.setData({ subtitle: '获取数据异常', loading: false })
        console.error(e)
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
   onReady: function (e) {
    
  },
  addorder(e){//下单
    let params = {};
    params.goods_id = e.currentTarget.dataset.goods_id;
    params.price = e.currentTarget.dataset.price;
    console.log(params)
    return app.getdata.fetchApi('order/addorder',params)
    .then(d => {
      if (d.status==1) {
        d.data.fail = app.wxPayFail;//取消回调
        app.wxPay(d.data);//接口返回支付参数，调用微信支付
      } else {
        app.showModal({content:d.message,confirm:app.turnBack});
      }
    })
    .catch(e => {
      this.setData({ subtitle: '获取数据异常', loading: false })
      console.error(e)
    })

  },
  showGoods(e){//商品详情
    let goods_id = e.currentTarget.dataset.goods_id;
    let price = e.currentTarget.dataset.price;
    let index = e.currentTarget.dataset.index;
    let is_best = e.currentTarget.dataset.is_best || 0;
    let detail = {};
    if(is_best){
      detail = this.goodsAarr.best_goods[index];
    }else{
      detail = this.goodsAarr.goods[index];
    }

    this.setData({ 
      detail:detail,
      openDetail:false,

    })
  },
  /**关闭物品详情*/
  closeDetail() {
      this.setData({openDetail: true})
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  onShareAppMessage: function (res) {//分享

    let that = this;
    let user_info = app.getUserInfo();
    let _path = '/pages/shouye/shouye';
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res)
      let stype = res.target.dataset.type;//分享类型
      if(stype==1){
        wx.showShareMenu({
          withShareTicket: true
        });//分享前置code

        console.log(_path)
        return {
          title: user_info.display_name+' 向你发起知识挑战，看谁知道面更广！',
          path: _path,
          success: function(res) {
            // 转发成功
            if (res.shareTickets) {
              wx.getShareInfo({// 获取转发详细信息
                shareTicket: res.shareTickets[0],
                success(res) {
                  console.log(res.encryptedData)
                  

                  let apiurl = 'user/share_points';//分享获取金币
                  let params = {is_group:1,share_type:'share_shop',encryptedData:res.encryptedData,iv:res.iv};
                  console.log(params);
                  app.getdata.fetchApi(apiurl,params)
                  .then(d => {
                    if (d.status==1){
                      app.showToast({title:'获取金币'+d.data.points});
                    }else{
                      app.showModal({content:d.message});return;
                    }
                  })
                  .catch(e => {
                    this.setData({ subtitle: '获取数据异常', loading: false })
                  })
                },
                fail() {},
                complete() {}
              });
            }else{
              if(!res.encryptedData){
                app.showModal({content:'分享到群才有奖励哦'});return;
              }
            }//分享end

          },
          fail: function(res) {
            // 转发失败
          }
        }
      }else{

      }
      
    }
    
  }



})